package com.dg.cuckoo.admin.service.impl;

import com.dg.cuckoo.admin.dto.proCarryOut.AppropriationDto;
import com.dg.cuckoo.admin.repository.AppropriationRepository;
import com.dg.cuckoo.admin.service.CompositeService;
import com.dg.cuckoo.admin.service.SuperiorService;
import com.dg.cuckoo.common.utils.PageModel;
import com.dg.cuckoo.core.ServiceException;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class CompositeServiceimpl implements CompositeService {
    @Autowired
    private SuperiorService service;
    @Autowired
    private AppropriationRepository repository;


    @Override
    public void save(String projectId, AppropriationDto dto) {
        service.powerfulSave(4,projectId,dto);
    }

    @Override
    public void update(String id, String projectId, AppropriationDto dto) {
        service.powerfulUpdate(4,id,projectId,dto);
    }

    @Override
    public String page(Integer page, Integer pageSize, String projectId) {
        if(projectId==null){
            throw new ServiceException("请选择项目");
        }
        Integer one = page;
        page = (page - 1) * pageSize;
        List<AppropriationDto> dtos = repository.findAll();
        List<AppropriationDto> info = repository.pageAll(projectId,page,pageSize);
        PageModel model = new PageModel(one, pageSize, dtos.size(), info);
        String amount = amount(dtos);
        JSONObject json = new JSONObject();
        json.put("PageModel",model);    //分页数据
        json.put("amount",amount);      //总金额
        return json.toString();
    }

    public String amount(List<AppropriationDto> dtos){
        BigDecimal bigDecimal = new BigDecimal("0");
        for(AppropriationDto a:dtos){
            bigDecimal.add(new BigDecimal(a.getAmount()));
        }
        return bigDecimal.toString();
    }
}
